package com.zym.controller;

import com.alibaba.excel.EasyExcel;
import com.zym.service.EmpService;
import com.zym.utils.EasyExcel.EasyExceGeneralDatalListener;
import com.zym.utils.EasyExcel.ExcelUtil;
import com.zym.vo.EmpVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "员工测试接口")
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private EmpService empService;

    @ApiOperation("excel导出")
    @GetMapping("/export")
    public void exportData() throws IOException {

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        empService.export();
        stopWatch.stop();
        System.out.println("共计耗时： " + stopWatch.getTotalTimeSeconds()+"S");
    }

    @ApiOperation("excel导入")
    @PostMapping("/import")
    public String importData(@PathVariable("file") MultipartFile file, HttpServletResponse response) throws IOException {
        System.out.println("1313");
        if (file.isEmpty()){
            return "文件不能为空";
        }
        String originalFilename = file.getOriginalFilename();
        if (!originalFilename.endsWith(".xlsx")){
            return "文件必须是.xlsx结尾的";
        }

        //记录开始读取Excel时间,也是导入程序开始时间
        long startReadTime = System.currentTimeMillis();
        System.out.println("------开始读取Excel的Sheet时间(包括导入数据过程):" + startReadTime + "ms------");
        //读取所有Sheet的数据.每次读完一个Sheet就会调用这个方法
        EasyExceGeneralDatalListener easyExceGeneralDatalListener = new EasyExceGeneralDatalListener(empService,response);
        EasyExcel.read(file.getInputStream(), EmpVo.class, easyExceGeneralDatalListener ).doReadAllSync();
        long endReadTime = System.currentTimeMillis();
        System.out.println("------结束读取Excel的Sheet时间(包括导入数据过程):" + endReadTime + "ms------");
        System.out.println("------读取Excel的Sheet时间(包括导入数据)共计耗时:" + (endReadTime-startReadTime) + "ms------");

        return "导入成功";
    }

}
